package com.jamin.aabill.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;


/**
 * 工具类<br>
 * ClassName:Utils
 *
 * @author   wangjieming
 * @version  1.0
 * @since    v1.0
 * @Date	 2012-7-25	上午8:29:58
 */
public class Utils {

	private static boolean DEBUG = Configuration.getDEBUG();

	/**
	 * 统一LOG开关<br>
	 * @method log
	 * @param TAG
	 * @param Message
	 * @throws 
	 * @since v1.0
	 */
	public static void log(String TAG, String Message) {
		if (DEBUG) {
			Log.i(TAG, Message);
		}
	}


	/**
	 * 检测网络状态是否为空<br>
	 * @method isNetworkAvailable
	 * @param context
	 * @return
	 * @throws 
	 * @since v1.0
	 */
	public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity == null) {
			return false;
		} else {
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					Log.i(String.valueOf(i), info[i].getTypeName());
					if (info[i].getState() == NetworkInfo.State.CONNECTED
							&& (info[i].getTypeName()
									.equalsIgnoreCase("MOBILE") || info[i]
											.getTypeName().equalsIgnoreCase("WIFI"))) {
						return true;
					}
				}
			}
		}
		return false;
	}



	/**
	 * 正则检测是否为电话号码<br>
	 * @method isPhoneNumberValid
	 * @param phoneNumber
	 * @return
	 * @throws 
	 * @since v1.0
	 */
	public static boolean isPhoneNumberValid(String phoneNumber) {
		String regExp = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
		Pattern p = Pattern.compile(regExp);
		Matcher m = p.matcher(phoneNumber);
		return m.matches();
	}



	/**
	 * 正则检测是否为邮箱地址<br>
	 * @method isMailboxValid
	 * @param mailBox
	 * @return
	 * @throws 
	 * @since v1.0
	 */
	public static boolean isMailboxValid(String mailBox) {
		Pattern p = Pattern
				.compile("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\\.([a-zA-Z0-9_-])+)+$");
		Matcher m = p.matcher(mailBox);
		return m.matches();
	}

	/**
	 * 正则检测是否为数字和字母<br>
	 * @method stringFilter
	 * @param str
	 * @return
	 * @throws 
	 * @since v1.0
	 */
	public static boolean stringFilter(String str) {
		// 只允许字母和数字
		String regEx = "[^a-zA-Z0-9]";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		return m.matches();
	}





	/**
	 * 如果SD存在返回路径，如果SD卡不存在返回null<br>
	 * @method getSDCardAbsolutePath
	 * @return
	 * @throws 
	 * @since v1.0
	 */
	public static String getSDCardAbsolutePath() {
		if ((Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))) {
			return Environment.getExternalStorageDirectory().getPath();
		}
		return null;
	}



	//-----------------------------MD5 32bit and 16bit
	/**
	 * 32位MD5加密算法<br>
	 * @method md5s32
	 * @param plainText
	 * @return
	 * @throws 
	 * @since v1.0
	 */
	public String md5s32(String plainText) {
		String str = null;
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(plainText.getBytes());
			byte b[] = md.digest();

			int i;

			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
			str = buf.toString();
			return str;
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return "";
		}
	}

	
	/**
	 * 16位MD5加密算法<br>
	 * @method md5s16
	 * @param plainText
	 * @return
	 * @throws 
	 * @since v1.0
	 */
	public String md5s16(String plainText) {
		String str = null;
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(plainText.getBytes());
			byte b[] = md.digest();

			int i;

			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
			str = buf.toString();
			return str.substring(8, 24);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return "";
		}
	}

	
	
	//--------------------------------------------dip和px互相转换
	/**
	 * dip转换成px
	 * @method dip2px
	 * @param context 
	 * @param dpValue
	 * @return
	 * @throws 
	 * @since v1.0
	 */
	public static int dip2px(Context context, float dpValue) {  
		final float scale = context.getResources().getDisplayMetrics().density;  
		return (int) (dpValue * scale + 0.5f);  

	}

	/**
	 * px转换成dip
	 * @method px2dip
	 * @param context
	 * @param pxValue
	 * @return
	 * @throws 
	 * @since v1.0
	 */
	public static int px2dip(Context context, float pxValue) {  
		final float scale = context.getResources().getDisplayMetrics().density;  
		return (int) (pxValue / scale + 0.5f);  
	}
}
